<template>
  <div class="page">
    <div class="page-top">
      <!-- 面包屑 -->
      <Hreadcrumb :hreadcrumbAry="hreadcrumbAry" />
    </div>
    <div class="page-content">
      <a-form-model layout="inline" style="margin-bottom: 20px" :form="form">
        <a-form-model-item label="订单类型">
          <a-select
            default-value="全部"
            style="width: 100px"
            v-model="form.databaseName"
          >
            <a-select-option value="SaleDeliveryUpdateHistory">
              销售出库
            </a-select-option>
            <a-select-option value="SaleBackUpdateHistory">
              销售退货
            </a-select-option>
            <a-select-option value="PurchaseOrderUpdateHistory">
              采购订单
            </a-select-option>
            <!-- <a-select-option value="PurchaseBackUpdateHistory">
              采购退货
            </a-select-option> -->
            <a-select-option value="TransVoucherUpdateHistory">
              调拨单
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="订单状态">
          <a-select
            default-value="全部"
            style="width: 300px"
            v-model="form.hasDeal"
          >
            <a-select-option value="-1"> 全部 </a-select-option>
            <a-select-option value="0"> 未处理 </a-select-option>
            <a-select-option value="1"> 处理失败 </a-select-option>
            <a-select-option value="2"> 处理成功 </a-select-option>
            <a-select-option value="3"> 采购入库回写成功 </a-select-option>
            <a-select-option value="4"> 反审核(取消)成功 </a-select-option>
            <a-select-option value="5"> 手动取消成功 </a-select-option>
            <a-select-option value="6">
              已关闭(非京东,自动关闭)
            </a-select-option>
            <a-select-option value="7">
              已关闭(自提单,地址非京东仓库,自动关闭)
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="创建时间">
          <a-range-picker
            show-time
            format="YYYY-MM-DD HH:mm:ss"
            v-model="timeValue"
            @change="bindTime"
          />
        </a-form-model-item>
        <a-form-model-item label="订单编号">
          <a-input v-model="form.code"></a-input>
        </a-form-model-item>
        <a-form-model-item label="京东单号">
          <a-input v-model="form.jdCode"></a-input>
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" html-type="submit" @click="onSubmit"> 提交 </a-button>
          <a-button style="margin-left: 15px" @click="bindReset">
            重置
          </a-button>
        </a-form-model-item>
      </a-form-model>
      <a-table
        :columns="columns"
        :data-source="data"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
      >
        <span slot="orderType">{{
          this.databaseName == "SaleDeliveryUpdateHistory"
            ? "销售出库"
            : this.databaseName == "SaleBackUpdateHistory"
            ? "销售退货"
            : this.databaseName == "PurchaseOrderUpdateHistory"
            ? "采购订单"
            : this.databaseName == "PurchaseBackUpdateHistory"
            ? "采购退货"
            : this.databaseName == "TransVoucherUpdateHistory"
            ? "调拨单"
            : ""
        }}</span>
        <span slot="OperateType" slot-scope="record">{{
          record == "Approve" ? "审核" : record == "Reject" ? "反审核" : ""
        }}</span>
        <span slot="HasDeal" slot-scope="record">{{
          record == "0"
            ? "未处理"
            : record == "1"
            ? "处理失败"
            : record == "2"
            ? "处理成功"
            : record == "3"
            ? "采购入库回写成功"
            : record == "4"
            ? "反审核（取消）成功"
            : record == "5"
            ? "手动取消成功"
            : record == "6"
            ? "已关闭（非京东单，自动关闭）"
            : record == "7"
            ? "已关闭（自提单，地址非京东仓库，自动关闭）"
            : ""
        }}</span>
        <span slot="options" slot-scope="text, record">
          <a-button
            type="primary"
            style="margin-right: 15px"
            :hidden="record.HasDeal != '5' && record.HasDeal != '1' ? true : false"
            @click="changeYYlogList(record, '0')"
            >重传</a-button
          >
          <a-button
            type="primary"
            :hidden="record.HasDeal != '0' ? true : false"
            @click="changeYYlogList(record, '5')"
            >拦截</a-button
          >
        </span>
      </a-table>
    </div>
  </div>
</template>
<script>
import Hreadcrumb from "@/components/Hreadcrumb";
import { getYYlogList, changeYYlogList } from "@/request/api";
export default {
  data() {
    return {
      loading: false,
      hreadcrumbAry: [
        { name: "首页", path: "/", query: {} },
        { name: "日志管理", path: "", query: {} },
        { name: "用友日志", path: "/KDlog", query: {} },
      ],
      columns: [
        {
          title: "单据类型",
          dataIndex: "",
          key: "",
          slots: { title: "单据类型" },
          scopedSlots: { customRender: "orderType" },
        },
        {
          title: "ERP单号",
          dataIndex: "Code",
          key: "Code",
        },
        {
          title: "操作状态",
          dataIndex: "OperateType",
          key: "OperateType",
          slots: { title: "操作状态" },
          scopedSlots: { customRender: "OperateType" },
        },
        {
          title: "运行时间",
          dataIndex: "OperateTime",
          key: "OperateTime",
          width: 180,
        },
        {
          title: "操作时间",
          dataIndex: "UpdateTime",
          key: "UpdateTime",
          width: 180,
        },
        {
          title: "单号",
          dataIndex: "JDNo",
          key: "JDNo",
        },
        {
          title: "状态",
          dataIndex: "HasDeal",
          key: "HasDeal",
          slots: { title: "状态" },
          scopedSlots: { customRender: "HasDeal" },
        },
        {
          title: "操作",
          dataIndex: "options",
          key: "options",
          slots: { title: "操作" },
          scopedSlots: { customRender: "options" },
          width: "200px",
        },
      ],
      pagination: { current: 1, size: 10 },
      data: [],
      form: {
        databaseName: "SaleDeliveryUpdateHistory",
        hasDeal: "-1",
        startTime: "",
        endTime: "",
        jdCode: "",
        code: "",
      },
      databaseName:'',
      timeValue: undefined,
    };
  },
  components: {
    Hreadcrumb,
  },
  created() {
    this.getYYlogList(1, 10);
  },
  methods: {
    async getYYlogList(p, ps) {
      this.loading = true;
      let { startTime, endTime, databaseName, hasDeal, jdCode, code } =
        this.form;
      let params =
        "p=" +
        (p - 1) +
        "&ps=" +
        ps + 
        "&dataSource=nnjx" +
        "&databaseName=" +
        databaseName +
        "&code=" +
        code +
        "&jdCode=" +
        jdCode +
        "&hasDeal=" +
        hasDeal +
        "&startTime=" +
        startTime +
        "&endTime=" +
        endTime;
      let { data } = await getYYlogList(params);
      console.log(data);
      if (data.success) {
        this.loading = false;
        let pagination = { ...this.pagination };
        pagination.total = data.content.total;
        this.pagination = pagination;
        this.data = data.content.list;
        this.databaseName = this.form.databaseName
      } else {
        this.loading = false;
        this.data = [];
      }
    },
    bindTime(date, dateString) {
      console.log(date, dateString);
      this.form.startTime = dateString[0];
      this.form.endTime = dateString[1];
    },
    bindReset() {
      this.form = {
        databaseName: "SaleDeliveryUpdateHistory",
        hasDeal: "-1",
        startTime: "",
        endTime: "",
        jdCode: "",
        code: "",
      };
      this.timeValue = undefined;
      this.getYYlogList(1, 10);
    },
    onSubmit(e) {
      e.preventDefault();
      console.log(this.form);
      this.getYYlogList(1, 10);
    },
    handleTableChange(pagination) {
      console.log(pagination);
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      this.pagination = pager;
      this.getYYlogList(pager.current, pager.size);
    },
    async changeYYlogList(record, hasDeal) {
      let param =
        "databaseName=" +
        this.form.databaseName +
        "&id=" +
        record.ID +
        "&hasDeal=" +
        hasDeal +
        "&updateBy=" +
        JSON.parse(localStorage.getItem("userInfo")).userName+
        "&dataSource=nnjx"
      let { data } = await changeYYlogList(param);
      if(data.success){
        this.getYYlogList(this.pagination.current,10);
        this.$message.success("操作成功");
      }else{
        this.$message.error(data.message);
      }
    },
  },
};
</script>
<style>
.page-content {
  background-color: #fff;
  padding: 25px;
}
</style>